<?php
namespace Home\Controller;
use Think\Controller;
class VtController extends MerchantController {

	public function _initialize(){
		parent::_initialize();	
	 	$this->model="site"; //表名
	 	$this->key="site_id";//排序键		 
	 	$this->title_index=L("vt");//选项卡标题	
	}	
	
	public function index(){
		//echo L("vt");
		$this->assign("main_title",$this->title_index);
		$oop=M($this->model);
		$con["site_mid"]=$this->merchant_info['mh_id'];
		$con['site_isactive']=1;
		$con['site_isvt']=1;
		$list=$oop->field(true)->where($con)->order($this->key." desc")->select();
		$this->assign("list",$list);		
		$this->display();
	}
	
	/**
	*VT交易也是普通交易的一种
	*/
	public function VtProcess(){
	//首先判断VT是否可用
	$this->merchant_info['mh_isvt'] || $this->error(L("operation_forbiden"));
		
	//根据获取的site_id,得到一些重要信息
	$list=M("site")->find(I("site_id"));
	//订单金额
	$order_amount=number_format(I("order_amount"),2,".","");
	//订单号，自动生成
	$oid=date("Ymd")."-".$this->merchant_info['mh_id']."-".$list["site_id"]."-VT-".date("hms");
	
   $hash_info=hash("sha256",$list["site_mid"].$list["site_id"].$oid.$order_amount.$this->web_config['web_currency'].$list["site_key"]);	
	
	$curlPost["order_amount"]=$order_amount;
	$curlPost["order_currency"]=$this->web_config['web_currency'];
	$curlPost["mid"]=$this->merchant_info["mh_id"];
	$curlPost["site_id"]=I("site_id");
	$curlPost["oid"]=$oid;
	$curlPost["hash_info"]=$hash_info;
	
	$curlPost["card_no"]=I("card_num");
	$curlPost["card_cvv"]=I("card_cvv");
	$curlPost["card_ex_year"]=I("ex_year");
	$curlPost["card_ex_month"]=I("ex_month");	
	
	$curlPost['bill_firstname']=I('bill_firstname');
	$curlPost['bill_lastname']=I('bill_lastname');
	$curlPost['bill_street']=I('bill_street');
	$curlPost['bill_city']=I('bill_city');
	$curlPost['bill_state']=I('bill_state');
	$curlPost['bill_country']=I('bill_country');
	$curlPost['bill_zip']=I('bill_zip');
	$curlPost['bill_phone']=I('bill_phone');
	$curlPost['bill_email']=I('bill_email');
	
	$curlPost['syn_url']=$this->web_config["web_domain"];
	$curlPost['asyn_url']=$this->web_config["web_domain"];
	$curlPost['source_ip']=get_client_ip();	
	
	$handle=new \Common\Common\GatewayHandle();
	//dump($curlPost);exit;
	$transaction=$handle->run($curlPost);
	//dump($transaction);exit;
	if($transaction=="")
		$this->error("invalid response,please try again");
	
	if($transaction['status']==0)
		$this->error($transaction['info']);
	
	switch($transaction['data']['order_status']){
		case 6://失败
		case 1://异常
			$this->error($transaction['info']);
		break;
		case 3://预授权成功
		case 2://成功
			$this->success($transaction['info']);
		break;
		default:
		$this->error("unknown response,please contact us");
	}
	}
	
}

